package com.macro.mall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.ResultCode;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.common.utils.Assist;
import com.macro.mall.mapper.TiTrainingMapper;
import com.macro.mall.mapper.TiTrainingSignRecordMapper;
import com.macro.mall.mapper.TiTrainingUserMapper;
import com.macro.mall.model.TiTraining;
import com.macro.mall.model.TiTrainingSignRecord;
import com.macro.mall.model.TiTrainingUser;
import com.macro.mall.service.TiTrainingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * TiTraining的服务接口的实现类
 *
 * @author gaodx
 */
@Slf4j
@Service
public class TiTrainingServiceImpl implements TiTrainingService {

    private boolean isDebugEnabled = true;

    @Autowired
    private TiTrainingUserMapper tiTrainingUserMapper;
    @Autowired
    private TiTrainingSignRecordMapper tiTrainingSignRecordMapper;
    @Autowired
    private TiTrainingMapper tiTrainingMapper;
    // TODO 当你看到这个方法时你应该创建一个工具类做通用的方法,定义自己的返回格式化

    @Override
    public List<TiTraining> find(String name, Integer pageNum, Integer pageSize) {
        //TODO这里可以做通过Assist做添加查询
        PageHelper.startPage(pageNum, pageSize);
        Assist assist = new Assist();
        List<TiTraining> result = null;
        try {
            if (StrUtil.isNotEmpty(name)) {
                assist.andLike("name ", "%" + name + "%");
            }
//            assist.setOrder(Assist.order("sort",false));
            assist.setOrder(Assist.order("create_time",false));

            result = tiTrainingMapper.selectTiTraining(assist);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行获取TiTraining数据集-->结果:", result);
        }
        return result;
    }

    @Override
    public TiTraining findOne(Integer id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过TiTraining的id获得TiTraining对象-->失败:id不能为空");
            }
            Asserts.fail("执行通过TiTraining的id获得TiTraining对象-->失败:id不能为空");
        }
        TiTraining result = null;
        try {
            result = tiTrainingMapper.selectTiTrainingById(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过TiTraining的id获得TiTraining对象-->结果:", result);
        }
        return result;
    }

    @Override
    public CommonResult saveNotNull(TiTraining value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行将TiTraining中属性值不为null的数据保存到数据库-->失败:对象不能为空");
            }
            Asserts.fail("失败:对象不能为空");
        }
        int result = 0;
        try {
            if(!CollectionUtils.isEmpty(value.getUserList())) {
                value.setUserAssign("1");
            } else {
                value.setUserAssign("0");
            }
            result = tiTrainingMapper.insertNotNullTiTraining(value);
            if (!CollectionUtils.isEmpty(value.getSignList())||!CollectionUtils.isEmpty(value.getUserList())) {
                if (value.getId()==null) {
                    TiTraining ttl = tiTrainingMapper.selectTiTrainingDescByObjSingle(value);
                    if (!StringUtils.isEmpty(ttl)){
                        value.setId(ttl.getId());
                    }
                }
                List<TiTrainingSignRecord> signList = value.getSignList();
                if (!CollectionUtils.isEmpty(signList)){
                    signList.stream().forEach(s -> {
                        s.setTrainingId(value.getId());
                        s.setCreateTime(new Date());
                    });
                    tiTrainingSignRecordMapper.insertTiTrainingSignRecordByBatch(signList);
                }
                List<TiTrainingUser> userList = value.getUserList();
                if (!CollectionUtils.isEmpty(userList)){
                    userList.stream().forEach(s -> {
                        s.setTrainingId(value.getId());
                        s.setTrainingName(value.getName());
                        s.setCreateTime(new Date());
                    });
                    tiTrainingUserMapper.insertTiTrainingUserByBatch(userList);
                }
            }
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行将TiTraining中属性值不为null的数据保存到数据库-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult updateNotNullById(TiTraining value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行通过TiTraining的id更新TiTraining中属性不为null的数据-->失败:对象为null");
            }
            Asserts.fail("执行通过TiTraining的id更新TiTraining中属性不为null的数据-->失败:对象为null");
        }
        int result = 0;
        try {
            if("00".equals(value.getTrainingStatus())) {
                //value.setStartTime();
            }
            if(!CollectionUtils.isEmpty(value.getUserList())) {
                value.setUserAssign("1");
            } else {
                value.setUserAssign("0");
            }
            value.setUpdateTime(new Date());
            result = tiTrainingMapper.updateNotNullTiTrainingById(value);
            tiTrainingSignRecordMapper.deleteTiTrainingSignRecordByTrainingId(value.getId());
            tiTrainingUserMapper.deleteTiTrainingUserByTrainingId(value.getId());
            if (value.getSignList()!=null && value.getSignList().size()>0) {
                List<TiTrainingSignRecord> signList = value.getSignList();
                signList.stream().forEach(s -> {
                    s.setTrainingId(value.getId());
                    s.setUpdateTime(new Date());
                });
                result = tiTrainingSignRecordMapper.insertTiTrainingSignRecordByBatch(signList);
            }
            if (value.getUserList()!=null && value.getUserList().size()>0) {
                List<TiTrainingUser> userList = value.getUserList();
                userList.stream().forEach(s -> {
                    s.setTrainingId(value.getId());
                    s.setTrainingName(value.getName());
                    s.setUpdateTime(new Date());
                });
                result = tiTrainingUserMapper.insertTiTrainingUserByBatch(userList);
            }
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过TiTraining的id更新TiTraining中属性不为null的数据-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult updateExamById(TiTraining value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("提交考卷回写试卷ID-->失败:对象为null");
            }
            Asserts.fail("提交考卷回写试卷ID-->失败:对象为null");
        }
        int result = 0;
        try {
            result = tiTrainingMapper.updateExamById(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("提交考卷回写试卷ID--->结果:", result);
        }
        return CommonResult.success(null);
    }


    @Override
    public CommonResult deleteById(Integer id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过TiTraining的id删除TiTraining-->失败:id不能为空");
            }
            Asserts.fail("执行通过TiTraining的id删除TiTraining-->失败:id不能为空");
        }
        int result = 0;
        try {
            result = tiTrainingMapper.deleteTiTrainingById(id);
            tiTrainingUserMapper.deleteTiTrainingUserByTrainingId(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过TiTraining的id删除TiTraining-->结果:", result);
        }
        return CommonResult.success(null);
    }


}